﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace CivCraft
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        SpriteFont spriteFont;
        //Texture2D defTile;
        Texture2D highlight_Norm;
        Texture2D highlight_Create;
        private const int TrgatFramerate = 60;
        private const int BackBufferWidth = 720;
        private const int BackBufferHeight = 636;

        private List<Unit> units = new List<Unit>();
        //private List<Texture2D> defTile = new List<Texture2D>[15]();
        Texture2D[] defTile = new Texture2D[16];
        private Map map;

        public int mouseX;
        public int mouseY;
            
        public int Height=7;
        public int Width=15;

        public int tsize = 48;

        private bool showHeight=false;
        private bool ismyTurn = true;



        private Random random = new Random(354668); // Arbitrary, but constant seed

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferHeight = BackBufferHeight;
            graphics.PreferredBackBufferWidth = BackBufferWidth;
            IsMouseVisible = true;
            map = new Map(Services);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            spriteFont = Content.Load<SpriteFont>("Fonts/Hud");
            for (int i = 1; i < 16; i++)
            {
                defTile[i] = Content.Load<Texture2D>("Tiles/defTile0" + i);
            }
            highlight_Norm = Content.Load<Texture2D>("Tiles/highlight_Norm");
            highlight_Create = Content.Load<Texture2D>("Tiles/highlight_Create");
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            
            HandleInput();
            map.Update(gameTime);

            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

        //    foreach (Unit unit in units) { }



            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        private void HandleInput()
        {
            KeyboardState kbstate = Keyboard.GetState();
            mouseX = Mouse.GetState().X;
             
            mouseY = Mouse.GetState().Y;
            mouseX = mouseX / 48;
            mouseY = mouseY / 48;

            if (Mouse.GetState().LeftButton == ButtonState.Pressed)
            {
                Window.Title = "X : " + mouseX + " Y : " + mouseY;
            }
            if (kbstate.IsKeyDown(Keys.Tab)) { showHeight = true; }
            else{ showHeight=false;}





        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.White);
            spriteBatch.Begin();
            // TODO: Add your drawing code here
            for (int y = 0; y < Height; y++)
            {
                for (int x = 0; x < Width; x++)
                {
                    Texture2D texture;
                    Texture2D dup;
                    dup = defTile[map.GetCells(x,y)];
                    texture = defTile[map.GetCells(x, y)];
                    
                    Vector2 position = new Vector2(x, y) * tsize;
                    Color hColor;
                    hColor = Color.White;
                    
                    spriteBatch.Draw(texture, position, hColor);
                    if (x == mouseX && y == mouseY)
                    {
                        dup = highlight_Norm;
                    }
                    spriteBatch.Draw(dup, position, hColor);
                    if (showHeight == true)
                    {
                        spriteBatch.DrawString(spriteFont, map.GetCells(x, y).ToString(), position, Color.Black);
                    }



                }
            }

            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
